﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Attributes;

namespace SharpDiary.Note
{
    /// <summary>
    /// 笔记编辑界面。
    /// </summary>
    [ModuleAttribute("c1498ab4-7301-4c30-8de1-e9516d6e9724", "NoteEditor", "Ray Shaw(demogodyou@gmail.com)(demogodyou@gmail.com)")]
    public partial class frmNoteEditor : SharpDiary.Core.Common.GuiModuleUIBase
    {
        #region Fields & Properties

        private NoteInfoListItemControl _itemControl;

        private NoteInfo _note;

        public NoteInfo Note
        {
            get { return _note; }
            set
            {
                _note = value;
                ShowNoteInfo();
            }
        }

        #endregion

        #region Constructors

        public frmNoteEditor()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        public override string GetSettingsKeyName()
        {
            return this.Name;
        }

        /// <summary>
        /// 显示笔记信息。
        /// </summary>
        private void ShowNoteInfo()
        {
            ClearDisplayNoteInfo();

            if (_note == null)
            {

                return;
            }

            txtTitle.Text = _note.Title;
            txtTags.Text = _note.Tags;
            txtContent.Text = _note.Content;
        }

        /// <summary>
        /// 清除笔记显示内容。
        /// </summary>
        private void ClearDisplayNoteInfo()
        {
            txtTitle.Clear();
            txtTags.Clear();
            txtContent.Clear();
        }

        public override void SetParameter(object parameter, object sender)
        {
            base.SetParameter(parameter, sender);

            _itemControl = sender as NoteInfoListItemControl;
            _note = parameter as NoteInfo;

            ShowNoteInfo();
        }

        /// <summary>
        /// 检查输入数据的合法性。
        /// </summary>
        /// <returns></returns>
        private bool CheckIfInputOk()
        {
            if (txtTitle.Text.Trim().Length == 0)
            {
                ShowMessage("请填写笔记标题。");
                txtTitle.Focus();
                txtTitle.SelectAll();

                return false;
            }

            if (txtContent.Text.Trim().Length == 0)
            {
                ShowMessage("请填写笔记正文。");
                txtContent.Focus();
                txtContent.SelectAll();

                return false;
            }

            return true;
        }

        /// <summary>
        /// 填充笔记信息实例属性值。
        /// </summary>
        /// <param name="note"></param>
        private void FillNoteInfo(NoteInfo note)
        {
            note.Title = txtTitle.Text;
            note.Tags = txtTags.Text;
            note.Content = txtContent.Text;
            note.CreatedDatetime = DateTime.Now;

        }

        /// <summary>
        /// 保存。
        /// </summary>
        private void Save()
        {
            if (!CheckIfInputOk()) { return; }

            if (_note == null || _note.Id == -1) //新增
            {
                _note = new NoteInfo();
                FillNoteInfo(_note);

                if (NoteInfo.Create<NoteInfo>(_note))
                {
                    if (_itemControl != null) { _itemControl.Note = _note; }

                    OwnerShell.NotifyMessage(this, new SharpDiary.Core.Common.MessageReceivedEventArgs(SharpDiary.Core.Common.MessageDefinition.Refresh,
                        "c1498ab4-7301-4c30-8de1-e9516d6e9724", "65b706fa-0052-40ae-bb19-38cbb4d93d87"));

                    ShowMessage("保存成功。");
                    DelayClose();
                }
                else
                {
                    ShowMessage("保存失败。");
                }
            }
            else //更新
            {
                FillNoteInfo(_note);
                if (NoteInfo.Update<NoteInfo>(_note))
                {
                    if (_itemControl != null) { _itemControl.Note = _note; }

                    ShowMessage("保存成功。");
                    DelayClose();
                }
                else
                {
                    ShowMessage("保存失败。");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Save();
        }

        #endregion


    }
}
